package com.example.apidocserver.domain.document.entity;

import com.example.apidocserver.infrastructure.base.BaseEntity;
import com.example.apidocserver.infrastructure.constants.SystemConstants;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.codec.binary.StringUtils;

import java.util.*;

/**
 * 接口入参对象(ApiParam)实体类
 *
 * @author makejava
 * @since 2023-03-07 22:37:23
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class ApiParam  extends BaseEntity {
    public ApiParam(Integer apiId, Integer paramType, Integer dataType) {
        this.apiId = apiId;
        this.paramType = paramType;
        this.dataType = dataType;
        this.enableFlag = SystemConstants.YES;
        this.nameAndFieldMap = new HashMap<>(8);
    }

    private Integer id;
    /**
     * 字段说明，接口文档展示
     */
    private String paramDesc;
    /**
     * 是否启用，0未启用，1启用
     */
    private Integer enableFlag;
    /**
     * 参数路径
     */
    private String paramPath;
    /**
     * 参数对象id
     */
    private Integer apiId;
    /**
     * 参数类型，header，path，query，body
     */
    private Integer paramType;
    /**
     * 数据类型，0入参，1返回参数
     */
    private Integer dataType;
    /**
     * 参数标题
     */
    private String title;
    /**
     * 参数层级
     */
    private Integer paramLevel;
    /**
     * 字段排序
     */
    private Integer orderNo;

    private List<ApiParamField> fields;

    @JsonIgnore
    private Set<String> fieldKeys;

    @JsonIgnore
    private Map<String, ApiParamField> nameAndFieldMap;

    public boolean equalsParam(ApiParam other) {
        if (this == other) {
            return true;
        }

        // GPT生成
        return Objects.equals(this.id, other.id)
                && Objects.equals(this.paramDesc, other.paramDesc)
                && Objects.equals(this.enableFlag, other.enableFlag)
                && Objects.equals(this.paramPath, other.paramPath)
                && Objects.equals(this.apiId, other.apiId)
                && Objects.equals(this.paramType, other.paramType)
                && Objects.equals(this.dataType, other.dataType)
                && Objects.equals(this.title, other.title)
                && Objects.equals(this.paramLevel, other.paramLevel)
                && Objects.equals(this.orderNo, other.orderNo);
    }

    /**
     * 增加字段名称对应名称字段说明
     *
     * @param field 字段说明
     */
    public void putField(ApiParamField field) {
        if (Objects.isNull(nameAndFieldMap)) {
            nameAndFieldMap = new HashMap<>(8);
        }
        nameAndFieldMap.put(field.getFieldName(), field);
    }
}

